package abio.XasML
{
	import abio.XasML.builder.context;
	import abio.XasML.event.centralDispatcher;
	import abio.XasML.tag.implAssembleTag_method;
	import abio.XasML.tag.implAssembleTag_property;
	
	import flash.events.Event;
	
	import org.spicefactory.lib.reflect.Metadata;
	
	public class global
	{
		public function global(){
			if(global._bCreating==false){	throw('global can have only one instance');}
			else{
				Metadata.registerMetadataClass(abio.XasML.tag.implAssembleTag_property);
				Metadata.registerMetadataClass(abio.XasML.tag.implAssembleTag_method);
			}
		}
		
		static private var _bCreating:Boolean=false;
		static private var _Singleton:global=null;
		static private function getInstance():global{
			if(global._Singleton==null){
				global._bCreating=true;
				global._Singleton=new global();
				global._bCreating=false;
			}
			return global._Singleton;
		}
		
		
		static private const CentralDispatcher:centralDispatcher=centralDispatcher.getInstance();
		static public function dispatchEvent(event:Object):Boolean
		{	return global.getInstance().dispatchEvent(event);}
		public function dispatchEvent(event:Object):Boolean{
			if(event is Event){
				return global.CentralDispatcher.dispatchEvent(event as Event);
			}else if(event is String){
				return global.CentralDispatcher.dispatchEvent(new Event(event as String));
			}else{
				return false;
			}
		}		
		static public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{	global.getInstance().addEventListener(type, listener, useCapture, priority, useWeakReference);}
		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void{
			CentralDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}		
		static public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{	global.getInstance().removeEventListener(type, listener, useCapture);}
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void{
			CentralDispatcher.removeEventListener(type, listener, useCapture);
		}
		
		static public function mxmlBuilder(builder:Object, parent:context=null):context
		{	return global.getInstance().mxmlBuilder(builder, parent);}
		public function mxmlBuilder(builder:Object, parent:context):context{
			return new context(builder, parent);
		}
	}
}